package com.yh.csx.bsf.flow.dao.dal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yh.csx.bsf.core.base.Ref;
import com.yh.csx.bsf.core.util.StringUtils;
import com.yh.csx.bsf.flow.provider.enums.FlowStateEnum;
import com.yh.csx.bsf.flow.provider.enums.TaskStateEnum;
import com.yh.csx.bsf.flow.core.db.ConvertUtil;
import com.yh.csx.bsf.flow.core.db.DbConn;
import com.yh.csx.bsf.flow.dao.dal.auto.t_flow_base_dal;
import com.yh.csx.bsf.flow.dao.model.auto.t_flow_model;
import com.yh.csx.business.core.util.DateUtils;
import lombok.val;
import lombok.var;

public class t_flow_dal extends t_flow_base_dal
{
    public Integer add2(DbConn conn, t_flow_model model)
    {
        add(conn,model);
        return ConvertUtil.objToInt(conn.executeScalar("select @@IDENTITY;",null));

    }
    public List<t_flow_model> getPage(DbConn db,Integer id, String title, String approve_node, String approve_by,String create_userno, String create_by, Integer flow_state,String model_name,Integer model_version,String create_time_from,String create_time_to, Integer pageindex, Integer pagesize, Ref<Integer> totalSize){
        val par = new ArrayList<>();
        val rs = new ArrayList<t_flow_model>();

        StringBuilder sb = new StringBuilder(" from t_flow where 1=1 ");
        if(id!=null&&id>=0){
            par.add(id);
            sb.append(" and id=?");
        }
        if(!StringUtils.isEmpty(title)){
            par.add(title);
            sb.append(" and title like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(approve_node)){
            par.add(approve_node);
            sb.append(" and approve_node like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(approve_by)){
            par.add(approve_by);
            sb.append(" and approve_by like concat('%', ?, '%')");
        }
        if(!StringUtils.isEmpty(create_userno)){
            par.add(create_userno);
            sb.append(" and create_userno like concat('%', ?, '%')");
        }

        if(!StringUtils.isEmpty(create_by)){
            par.add(create_by);
            sb.append(" and create_by like concat('%', ?, '%')");
        }
        if(flow_state!=null&&flow_state>=0){
            par.add(flow_state);
            sb.append(" and flow_state=?");
        }
        if(!StringUtils.isEmpty(model_name)){
            par.add(model_name);
            sb.append(" and model_name like concat('%', ?, '%')");
        }
        if(model_version!=null){
            par.add(model_version);
            sb.append(" and model_version=?");
        }
        if(!StringUtils.isEmpty(create_time_from)){
            par.add(DateUtils.parse(create_time_from,"yyyy-MM-dd HH:mm:ss"));
            sb.append(" and create_time > ?");
        }
        if(!StringUtils.isEmpty(create_time_to)){
            par.add(DateUtils.parse(create_time_to,"yyyy-MM-dd HH:mm:ss"));
            sb.append(" and create_time < ?");
        }

        String sql = "select * "+sb.toString()+" order by id desc "+String.format(" limit %s,%s",(pageindex-1)*pagesize,pagesize);
        String countSql = "select count(0) "+sb.toString();
        val ds = db.executeList(sql, par.toArray());
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(createModel(dr));
            }
        }
        totalSize.setData(ConvertUtil.objToInt(db.executeScalar(countSql,par.toArray())));
        return rs;
    }    

    public List<Integer> getNoTasksFlow(DbConn conn)
    {
        val rs = new ArrayList<Integer>();
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.id,count(0) as num from t_flow s,t_task t where s.id=t.flow_id and s.flow_state=? and t.task_state=? group by s.id having num=0;");
        var ds = conn.executeList(stringSql.toString(), new Object[]{FlowStateEnum.waiting.Value, TaskStateEnum.waiting.Value});
        if (ds != null && ds.size() > 0)
        {
            for (Map<String,Object> dr : ds)
            {
                rs.add(ConvertUtil.objToInt(dr.get("id")));
            }
        }
        return rs;
    }
    public Integer getCountOfUsed(DbConn conn, String model_name){
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(model_name);
        Par.add(FlowStateEnum.close);
        Par.add(FlowStateEnum.done);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select count(0) from t_flow s where s.model_name=? and s.flow_state not in (?,?)");
        Integer count = ConvertUtil.objToInt(conn.executeScalar(stringSql.toString(), Par.toArray()));
        return count;
    }
    public Integer getCountOfUsed(DbConn conn, Integer id){
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(id);
        Par.add(FlowStateEnum.close);
        Par.add(FlowStateEnum.done);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select count(0) from t_flow s where s.model_id=? and s.flow_state not in (?,?)");
        Integer count = ConvertUtil.objToInt(conn.executeScalar(stringSql.toString(), Par.toArray()));
        return count;
    }
    public String getTitle(DbConn conn, int id)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(id);
        StringBuilder stringSql = new StringBuilder();
        stringSql.append("select s.title from t_flow s where s.id=?");
        String title = (String)conn.executeScalar(stringSql.toString(), Par.toArray());
        return title;
    }

    public boolean updateFlowApprove(DbConn conn, int flowid,String approve_node,String approve_userno,String approve_by)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(approve_node);
        Par.add(approve_userno);
        Par.add(approve_by);
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set approve_node=?,approve_userno=?,approve_by=? where id=?",  Par.toArray());
        return rev == 1;

    }
    
    public boolean updateFlowInfo(DbConn conn, int flowid,String flow_variable_json,String updateBy)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flow_variable_json);
        Par.add(updateBy);
        Par.add(new Date());
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set flow_variable_json=?,update_by=?,update_time=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateFlowVariable(DbConn conn, int flowid,String flow_variable_json)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flow_variable_json);
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set flow_variable_json=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateFlowState(DbConn conn, int flowid,int flow_state)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(flow_state);
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set flow_state=?,flow_error='' where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateModel(DbConn conn, int flowid,int model_id,String model_name,String model_title,Integer model_version)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(model_id);Par.add(model_name);Par.add(model_title);Par.add(model_version);
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set model_id=?,model_name=?,model_title=?,model_version=? where id=?",  Par.toArray());
        return rev == 1;

    }

    public boolean updateFlowError(DbConn conn, int flowid,String flow_error)
    {
        ArrayList<Object> Par = new ArrayList<Object>();
        Par.add(FlowStateEnum.error.Value);
        Par.add(flow_error);
        Par.add(flowid);
        int rev = conn.executeSql("update t_flow set flow_state=?,flow_error=? where id=?",  Par.toArray());
        return rev == 1;

    }
}
